import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { Message } from 'element-ui'

Vue.use(VueRouter)

import index from '../views/index'

const routes = [
  // {
  //   path: '/',
  //   redirect: '/',
  // },
  {
    path: '*',
    redirect: '/',
  },
  {
    path: '/',
    name: 'index',
    component: index,
    meta: {
      title: '首页',
      keepAlive: true,
    },
    // children:[
    //   {
    //     path: '/mian',
    //     name: 'mian',
    //     component: () => import('../components/Main/Main.vue'),
    //   },
    // ],
    // // 默认二级路由
    // redirect: '/mian'
  },
  {
    path: '/dashboard',
    name: 'dashboard',
    component: () => import('@/views/dashboard.vue'),
    meta: { title: '主要页面一' },
    children: [
      {
        path: 'MuSic',
        name: 'MuSic',
        component: () => import('@/views/dashboard/MuSic.vue'),
        meta: { title: '音乐模块' },
      },
      {
        path: 'NetworkDisk',
        name: 'NetworkDisk',
        component: () => import('@/views/dashboard/NetworkDisk.vue'),
        meta: { title: '网盘组件' },
        // 路由独享的守卫
        beforeEnter: (to, from, next) => {
          if (store.state.userinfo.jurisdiction !== 1) {
            Message.warning('你没有权限！')
            return
          }
          next()
        },
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue'),
  },
]

const router = new VueRouter({
  mode: process.env.VUE_APP_ROUTER_MODE,
  base: process.env.BASE_URL,
  routes,
})

/**
 * 全局前置守卫
 * 使用全局前置守卫检测保存的用户信息
 * 解决刷新之后 vuex中的用户信息丢失
 */
/* router.beforeEach((to, from, next) => {
  const uestinfo = local.get('userinfo_Token')
  if (uestinfo) {
    store.commit('getuserToken', uestinfo)
  }
  next()
}) */

export default router
